﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BugNET.UserInterfaceLayer;
using BugNET.BLL;
using BugNET.Common;
using BugNET.Entities;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.DataVisualization.Charting;

namespace BugNET.DashBoardPages
{
    public partial class DashBoard_Status : BasePage
    {
        Project CurrentProject;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserManager.HasPermission(Convert.ToInt32(Request.Params["pid"]), Common.Permission.ViewDashboard.ToString()))
                Response.Redirect("~/Errors/AccessDenied.aspx");


            if (!IsPostBack)
            {
                BindOptions();
                txtStartDate.Text = DateTime.Today.AddDays(-7).ToShortDateString();
                txtEndDate.Text = DateTime.Today.ToShortDateString();
            }

            Page.Title = GetLocalResourceObject("PageTitle").ToString();
            //Set 3D View
            Chart.ChartAreas["ChartArea"].Area3DStyle.Enable3D = chkIs3dView.Checked;

            // Set series chart type
            if (Bar.Checked)            
                Chart.Series[0].ChartType = SeriesChartType.Bar;            
            else if (Column.Checked)            
                Chart.Series[0].ChartType = SeriesChartType.Column;
            

            // Set Gridlines Visibility           
            Chart.ChartAreas["ChartArea"].AxisX.MajorGrid.Enabled = chkIsGridlineVisible.Checked;
            Chart.ChartAreas["ChartArea"].AxisY.MajorGrid.Enabled = chkIsGridlineVisible.Checked;

            //Set Drawing Style
            Chart.Series[0]["DrawingStyle"] = DrawingStyle.SelectedValue;

            // Set point width of the series
            Chart.Series[0]["PointWidth"] = BarWidthList.SelectedItem.Text;

            // Set 3d style as right angled
            Chart.ChartAreas["ChartArea"].Area3DStyle.IsRightAngleAxes = RightAngledAxis.Checked;

            // Set chart area 3D rotation
            if (RotateX.SelectedItem.Text != "")
                Chart.ChartAreas["ChartArea"].Area3DStyle.Inclination = int.Parse(RotateX.SelectedItem.Text);

            if (RotateY.SelectedItem.Text != "")
                Chart.ChartAreas["ChartArea"].Area3DStyle.Rotation = int.Parse(RotateY.SelectedItem.Text);

            Chart.ChartAreas["ChartArea"].AxisX.LabelStyle.IsEndLabelVisible = true;
            Chart.Series[0].IsValueShownAsLabel = true;

            ProjectId = Request.QueryString.Get("pid", 0);
           

            // If don't know project or issue then redirect to something missing page
            if (ProjectId == 0)
                ErrorRedirector.TransferToSomethingMissingPage(Page);

            CurrentProject = ProjectManager.GetById(ProjectId);

            if (CurrentProject == null)
            {
                ErrorRedirector.TransferToNotFoundPage(Page);
                return;
            }

            GenerateGraph();
        }       

        /// <summary>
        /// Generates Dashboard Graph
        /// </summary>
        public void GenerateGraph()
        {
            int mileStoneId = DropMilestone.SelectedValue;
            int priorityId = DropPriority.SelectedValue;
            int categoryId = DropCategory.SelectedValue;
            int typeId = DropIssueType.SelectedValue;
            DateTime StartDate = Convert.ToDateTime(txtStartDate.Text);
            DateTime EndDate = Convert.ToDateTime(txtEndDate.Text).AddDays(1);
            List<Entities.DashBoard> lstDashBoardData = DashBoardManager.GetDashBoard_Status(ProjectId, mileStoneId, priorityId, categoryId, typeId, StartDate, EndDate);


            for (int pointIndex = 0; pointIndex < lstDashBoardData.Count; pointIndex++)
            {
                Chart.Series[0].Points.AddY(lstDashBoardData[pointIndex].Count);
                Chart.Series[0].Points[pointIndex].AxisLabel = lstDashBoardData[pointIndex].StatusName;
            }
                       
        }        
       
        /// <summary>
        /// Binds the options.
        /// </summary>
        private void BindOptions()
        {
            List<ITUser> users = UserManager.GetUsersByProjectId(ProjectId, true);

            string defaultname = "-- ALL --";
            //Get Type
            DropIssueType.DataSource = IssueTypeManager.GetByProjectId(ProjectId);
            DropIssueType.DataBind();
            DropIssueType.RenameDefault(defaultname);

            //Get Priority
            DropPriority.DataSource = PriorityManager.GetByProjectId(ProjectId);
            DropPriority.DataBind();
            DropPriority.RenameDefault(defaultname);

            //Get categories
            var categories = new CategoryTree();
            DropCategory.DataSource = categories.GetCategoryTreeByProjectId(ProjectId);
            DropCategory.DataBind();
            DropCategory.RenameDefault(defaultname);

            //Get milestones
            DropMilestone.DataSource = MilestoneManager.GetByProjectId(ProjectId);
            DropMilestone.DataBind();
            DropMilestone.RenameDefault(defaultname);

        }

        /// <summary>
        /// Reset Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }
    }
}